﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Goods
{
    /// <summary>
    /// Interaction logic for GoodsCleanUserControl.xaml
    /// </summary>
    public partial class GoodsCleanUserControl : UserControl, IPlugin
    {
        public string Title
        {
            get { return "商品清理"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        public GoodsCleanUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.dpEnd.Value = DateTime.Now.AddDays(-90);
        }


        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var time = this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value;

                if (Debugger.IsAttached == false && DateTime.Now.Subtract(time).TotalDays < 60)
                {
                    throw new Exception("查询时间不能小于60天");
                }

                this.pbBar.Parameters.Clear();
                this.pbBar.Parameters.Add("vendor", this.tbVendor.Text.Trim());
                this.pbBar.Parameters.Add("number", this.tbNumber.Text.Trim());
                this.pbBar.Parameters.Add("lastSellTime", time);
                this.pbBar.StartPage();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var data = ServiceContainer.GetService<ShoesService>().GetByAll(e.GetParameter<string>("vendor"), e.GetParameter<string>("number"), 0, 2, DateTime.MinValue, e.GetParameter<DateTime>("lastSellTime"), e.CurrentPage - 1, e.PageSize);
            this.pbBar.Total = data.Total;
            this.pbBar.CurrentCount = data.Datas.Count;
            this.dgvGoods.ItemsSource = data.Datas.Select(obj => new GoodFixViewModel { Source = obj, State = "" }).ToArray();
        }

        private void Delete(GoodFixViewModel[] items)
        {
            var ss = ServiceContainer.GetService<ShoesService>();
            foreach (var og in items)
            {
                try
                {
                    if (og.Source.Id <= 0)
                    {
                        og.State = "已删除";
                    }
                    else
                    {
                        ss.Delete(og.Source.Id);
                        og.Source.Id = 0;
                        og.State = "已删除";
                    }
                }
                catch (Exception exe)
                {
                    og.State = exe.Message;
                }
                App.DoEvents();
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var ss = ServiceContainer.GetService<ShoesService>();
                if (this.dgvGoods.ItemsSource == null)
                {
                    throw new Exception("没有数据需要删除");
                }
                GoodFixViewModel[] items;
                string msg = "";
                msg = "是否删除所有商品?";
                items = this.dgvGoods.ItemsSource as GoodFixViewModel[];
                if (MessageBox.Show(msg, "警告", MessageBoxButton.YesNo, MessageBoxImage.Error) != MessageBoxResult.Yes)
                {
                    return;
                }
                this.Delete(items);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void btnDeletePart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var ss = ServiceContainer.GetService<ShoesService>();
                if (this.dgvGoods.ItemsSource == null)
                {
                    throw new Exception("没有数据需要删除");
                }

                if (this.dgvGoods.SelectedItems == null || this.dgvGoods.SelectedItems.Count < 1)
                {
                    throw new Exception("没有选择数据");
                }

                GoodFixViewModel[] items;
                string msg = "";
                msg = "是否删除选择商品?";
                items = this.dgvGoods.SelectedItems.OfType<GoodFixViewModel>().ToArray();
                if (MessageBox.Show(msg, "警告", MessageBoxButton.YesNo, MessageBoxImage.Error) != MessageBoxResult.Yes)
                {
                    return;
                }
                this.Delete(items);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
